package org.tlipoca.smsteamstore.task;

import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smdomain.dto.SteamAppListResponseDTO;
import org.tlipoca.smsteamstore.mapper.SteamStoreMapper;
import org.genntii.smcommon.constant.SteamApiURLConstant;
import org.genntii.smcommon.constant.SteamConstant;
import org.genntii.smcommon.utils.HttpClientUtil;
import org.genntii.smdomain.dto.SteamAppListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Map;

@Slf4j
public class SteamStoreTask {

    @Resource
    private SteamStoreMapper steamStoreMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    public static final String STEAM_APP_DETAIL_MONGODB_COLLECTION = "SteamAppDetail";

    // SpringTask 每日04:00 自动执行
//    @Scheduled(cron = " 0 0 4 * * ? ")
    public void getSteamAppList() {
        log.info("开始清空Steam_Store数据库...");
//        steamStoreMapper.deleteTable();

        log.info("开始获取SteamApp列表...");
        Integer lastAppid = 0;
        SteamAppListDTO appListResultJson = null;

        // 获取全部游戏与可下载内容
        while(lastAppid == 0 || appListResultJson.getHave_more_results()) {
            log.info("获取App列表 初始id: {}", lastAppid);
            // 获取接口数据
            Map<String, Object> map = Map.of("key", SteamConstant.STEAM_KEY, "include_dlc", true, "max_results", 50000, "last_appid", lastAppid);
            appListResultJson = JSON.parseObject(HttpClientUtil.doGet(SteamApiURLConstant.GET_APP_LIST, map), SteamAppListResponseDTO.class).getResponse();

            // 批量插入数据库
            steamStoreMapper.insertBatch(appListResultJson.getApps());

            // 没有更多数据 跳出循环
            if(appListResultJson.getHave_more_results() == null || appListResultJson.getLast_appid() == null) break;
            lastAppid = appListResultJson.getLast_appid();
        }

        log.info("App列表获取完成");
        log.info("开始更新游戏列表...");

        lastAppid = 0;

        // 获取游戏列表 将数据库中DLC字段批量更改为false
        while(lastAppid == 0 || appListResultJson.getHave_more_results()) {
            log.info("获取游戏列表 初始id: {}", lastAppid);
            // 获取接口数据
            Map<String, Object> map = Map.of("key", SteamConstant.STEAM_KEY, "include_dlc", false, "max_results", 50000, "last_appid", lastAppid);
            appListResultJson = JSON.parseObject(HttpClientUtil.doGet(SteamApiURLConstant.GET_APP_LIST, map), SteamAppListResponseDTO.class).getResponse();

            // 批量更新数据库
            steamStoreMapper.updateDLCBatch(appListResultJson.getApps());

            // 没有更多数据 跳出循环
            if(appListResultJson.getHave_more_results() == null || appListResultJson.getLast_appid() == null) break;
            lastAppid = appListResultJson.getLast_appid();
        }

        log.info("更新游戏列表完成");
    }

    // SpringTask 执行一次
//    @Scheduled(fixedDelay = Long.MAX_VALUE)
//    public void getAppDetail() throws InterruptedException {
//
//        ArrayList<SteamStore> steamStoreList = steamStoreMapper.selectList();
//
//        for (SteamStore item : steamStoreList) {
//            Map<String, Object> map = Map.of("appids", item.getAppid());
//            log.info("获取App详情, AppID: {}", item.getAppid());
//
//            // 获取游戏详情
//            String jsonResult = HttpClientUtil.doGet(SteamApiURLConstant.GET_APP_DETAIL, map);
//
//            // Json数据序列化为Java类
//            SteamAppDetail steamAppDetail = JSON.parseObject(jsonResult, SteamAppDetail.class);
//
//            SteamStore steamStore = SteamStore.builder()
//                    .id(item.getId())
//                    .appid(item.getAppid())
//                    .name(item.getName())
//                    .short_description(steamAppDetail.getShort_description())
//                    .detail_description(steamAppDetail.getDetailed_description())
//                    .isDlc(item.isDlc())
//                    .header_image(steamAppDetail.getHeader_image())
//                    .capsule_image(steamAppDetail.getCapsule_image())
//                    .requiredAge(steamAppDetail.getRequired_age())
//                    .is_free(steamAppDetail.getIs_free())
//                    .build();
//
//            // 游戏详情更新到Mysql
//            steamStoreMapper.update(steamStore, new QueryWrapper<SteamStore>().eq("id", steamStore.getId()));
//
//            // 游戏详情插入MongoDB
//            mongoTemplate.insert(steamAppDetail);
//
//            Thread.sleep(1000L);
//        }
//
//    }

}
